// pages/index/mine_price.js
Page({

 
  data: {    price:'00.00',taocan:[],appId:"",show:false,
  nonceStr:"",
  package:"",
  paySign:"",
  signType:"",
  timeStamp:"",last:'',
          
  },

  onLoad: function (options) {
          this.getmoney()
          this.gettaocan()
          
  },
  getmoney(){
    wx.request({
      url: 'https://zujieba.com/Api/price/index.html',
      data:{uid:wx.getStorageSync('uid')},
      success:(res)=>{
           this.setData({
             price:res.data.data.price,
           })
         
           console.log(res);
           
           
        
      }
    })
  },
  gettaocan(){
    wx.request({
      url: 'https://zujieba.com/Api/gift/gift_order_list',
      method:'POST',
      success:(res)=>{
         console.log(res);
     let taocan=res.data.data
     this.setData({
       taocan
     })
         
      }
    })
  },
  //购买礼包
  paylibao(e){
    console.log(e);
    this.throttle(()=>{
      wx.request({
        url: 'https://zujieba.com/Api/gift/add_gift_order.html',
        data:{id:e.currentTarget.dataset.id,uid:wx.getStorageSync('uid')},
        method:'POST',
        success:(res)=>{
          
           if(res.data.success==1){
             //生成订单号
              let code=res.data.data
              this.getpaylist(code)
 
           }else{
             wx.showToast({
               title: '生成订单失败',
               duration:2000
             })
           }
          
          
        }
      })
    },1500)
     
  },
  getpaylist(code){
    
      
      
      wx.request({
        url: 'https://zujieba.com/Api/gift/pay_gift_order.html',
        data:{code:code},
        method:'POST',
        success:(res)=>{
          console.log(code);
          
          console.log(res);
          this.setData({
            appId:res.data.appId,
            nonceStr:res.data.nonceStr,
            package:res.data.package,
            paySign:res.data.paySign,
            signType:res.data.signType,
            timeStamp:res.data.timeStamp,
            
          })
          this.getpay()
         
        }
      })
    },
     //支付
   getpay(){
    wx.requestPayment(
      {
      'timeStamp': this.data.timeStamp,
      'nonceStr': this.data.nonceStr,
      'package': this.data.package,
      'signType': this.data.signType,
      'paySign': this.data.paySign,
      'success':(res)=>{
        console.log(res);
          
            wx.showToast({
              title: '支付成功',
              icon:'success',
              duration:2000
            })
            this.getmoney()
            this.getmima()

          
              
      }, })
  },
  getmima(){
     wx.request({
       url: 'https://zujieba.com/Api/gift/pass.html',
       data:{uid:wx.getStorageSync('uid')},
       method:'POST',
       success:(res)=>{
         console.log(res);
         if(res.data.success==1){
            return
         }else{
          this.setData({
             show:true
          })
         }
         
       }
     })
  },
  navpaypass(){
     wx.navigateTo({
       url: '/pages/mine/paypass/paypass?phone='+wx.getStorageSync('phone'),
     })
  },
  shanchu(){
    this.setData({
      show:false
    })
  },
  onHide(){
    this.setData({
      show:false
    })
  },
  //节流
  throttle:function(func,wait){
    console.log(func,wait);
    
    let last =this.data.last
    console.log(last);
    
    let current_time= +new Date()
    if(current_time-last>wait){
      console.log(1111);
        console.log(current_time,current_time-last);
        
        func()
        last=+new Date()
        this.setData({
          last
        })
    }
      
       
    
     
  },
  navdianpu(){
    wx.navigateTo({
      url: '/pages/mine/dianpu/dianpu',
    })
  },
  getstoreprice(){
    wx.request({
      url: 'https://zujieba.com/Api/price/storeprice',
      data:{uid:wx.getStorageSync('uid')},
      method:'get',
      success:(res)=>{
        console.log(res);
        this.setData({storeprice:res.data.data})
        
      }
    })
  },
  onShow:function(){
    this.getstoreprice()
  }
  
 
})